﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using ServerApi.Controllers;
using ServerApi.Models.Menu;
using TuyennvLibrary.Utl;

namespace ServerApi.administrator
{
    public partial class TypeRoleManager : System.Web.UI.Page
    {
        public string title = "Thêm mới loại quyền";
        public int stt = 1;
        public string message = "";
        TypeRoleModel typeModel = new TypeRoleModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            //AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int id = int.Parse(Request["id"]);
                    loadInfo(id);
                    title = "Cập nhật loại quyền";
                }
                else
                {
                    title = "Thêm mới loại quyền";
                }
                loadData();
            }
        }
        private void loadInfo(int id)
        {
            TypeRole obj = typeModel.GetByID(id);
            loadInfo(obj);
        }
        private void loadInfo(TypeRole obj)
        {
            txtName.Text = obj.Name;
        }
        private void loadData()
        {
            DataTable dt = typeModel.GetListAll();
            GrvList.DataSource = dt;
            GrvList.DataBind();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                TypeRole obj = new TypeRole();
                obj.Name = txtName.Text;
                obj.Weight = typeModel.GetMaxWeight() + 1;
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    obj.Id = int.Parse(Request["id"]);
                    typeModel.Update(obj);
                    message = Utilities.getSuccessMessage("Cập nhật loại quyền thành công");
                }
                else
                {
                    typeModel.Insert(obj);
                    message = Utilities.getSuccessMessage("Thêm mới loại quyền thành công");
                }
                loadData();
            }
            catch (Exception ex)
            {
                message = Utilities.getErrorMessage("Lỗi: " + ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            loadData();
        }
        protected bool UpdateWeight(int id, int weight)
        {
            List<TypeRole> source = typeModel.GetListBy(typeModel.GetListAll());
            typeModel.ChangeWeight(source, id, weight);
            typeModel.UpdateWeightForList(source, "weight", "id");
            return true;
        }
        protected void GrvList_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            string commandname = e.CommandName.ToString();
            if (commandname == "Sort")
            {
                try
                {
                    int id = int.Parse(e.CommandArgument.ToString());
                    //category_film catefilm = cate_filmModel.GetByID(id);
                    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    TextBox t = (TextBox)row.FindControl("txt_thutu");
                    int weight = int.Parse(t.Text);
                    UpdateWeight(id, weight);
                    //catefilm.film_weight = int.Parse(t.Text);
                    txtNameSearch.Text = string.Empty;
                }
                catch (Exception ex)
                {

                    Utilities.Alert(Page, this.GetType(), ex.Message);
                }
            }
            loadData();
        }

    }
}